Vue.component('show-detail', {
    template: `<el-dialog title="明细查询" :visible.sync="visibleDialog" width="80vw">
    <el-table :data="tableData" stripe border min-height="50vh" style="width: 100%">
        <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
        <el-table-column align="center" prop="Supplier_Article_SerialNumber" show-overflow-tooltip label="供应商品内码"></el-table-column>
        <el-table-column align="center" prop="Supplier_Name" show-overflow-tooltip label="供应商名称"></el-table-column>
        <el-table-column align="center" prop="Article_Name" show-overflow-tooltip label="商品名称"></el-table-column>
        <el-table-column align="center" prop="Specifications_Name" show-overflow-tooltip label="规格名称"></el-table-column>
        <el-table-column align="center" prop="Quantity" show-overflow-tooltip label="每日库存数量"></el-table-column>
    </el-table>
    <el-pagination style="margin-top: 5px;" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="form.page" :page-sizes="[10, 20, 30, 50]"
        :page-size="form.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:visible',false)">返回</el-button>
    </span></el-dialog>`,
    props: ['id','visible'],
    data() {
        return {
            visibleDialog: false,
            refreshDialog: false,
            tableData: [],
            form: {
                SQLSerialNumber: "P_business_supplier_article_stock_detail",
                PaginationNumber: 1,
                RowAmount: 10,
                ExtensionCondition: '',
                Condition: ''
            },
            total: 0,
        }
    },
    watch: {
        visible() {
            this.visibleDialog = this.visible
        },
        visibleDialog(val) {
            this.$emit('update:visible', val)
            if (val) {
                this.form.Condition = window.btoa(unescape(encodeURIComponent(this.id)));
                this.getList(this.form)
            }
        },
    },
    methods: {
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
            this.getList(this.form)
        },
        getList(data) {
            const that = this
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    that.refreshDialog = false;
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        }
    }
})
new Vue({
    el: '#app',
    data() {
        return {
            form: {
                SQLSerialNumber: "P_business_supplier_article_stock",
                PaginationNumber: 1,//页数
                RowAmount: 10,//每页数据条数
                Condition:'',
                ExtensionCondition:'',
            },
            total:0,
            tableData: [],	//表格数据
            dialogVisible:false,
            Supplier_Article_Stock_SerialNumber:'',
            loading:false,
        }
    },
    mounted() {
        this.getList(this.form);//前端分页
    },
    methods: {
        onSerach() {			//查询方法
            this.form.PaginationNumber = 1;
            this.getList(this.form)
        },
        Add() {
            window.location.href = './add.html'
        },
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
            this.getList(this.form)
        },
        getList(form) {//换页方法
            const that = this
            let data = deepClone(form);
            // data.Condition = window.btoa(unescape(encodeURIComponent("%"+data.Supplier_Name+"%")))
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        downloadTemplate(command){
            if (command == "stock") {
                window.location.href = './download/导入供应库存模板.xlsx';
            } 
        },
        importTemplate(command){
            if (command == "stock") {
                window.location.href = './import.html';
            } 
        },
        Daily(){
            const that = this;
            this.$confirm('此操作将生成今日执行单（已存在的不会更新）, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.loading = true;
                $.ajax({//更新今日执行单
                    headers: requestHeaders(),
                    url: `${BASE_URL2}/article/generateDailyInventory`,
                    type: 'GET',
                    complete: function (XMLHttpRequest) {
                        that.loading = false;
                        if (XMLHttpRequest.status == 200) {
                            that.$message.success("今日库存单更新成功")
                        } else {
                            that.$message.error("今日库存单更新失败")
                        }
                    }
                })
            }).catch(() => {});
        },
        Show(Supplier_Article_Stock_SerialNumber) {
            this.Supplier_Article_Stock_SerialNumber = Supplier_Article_Stock_SerialNumber
            this.dialogVisible = true
        },
        Remove(row) {
            const that = this;
            this.$confirm('警告！此操作将删除该库存单, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                row.Row_Status = '1';
                $.ajax({
                    headers:requestHeaders(),
                    url: `${BASE_URL2}/generalBusiness/generalSave`,
                    type: 'POST',
                    data: {
                        Data: JSON.stringify({ Modify: { business_supplier_article_stock: [row] } })
                    },
                    success: function (res) {
                        if (res.Status === 'success') {
                            that.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            that.getList(that.form);
                        } else {
                            that.$message.error("删除失败，请稍后再试")
                        }
                    },
                    complete: function (XMLHttpRequest) {
                        that.loading = false
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json"
                })
            }).catch(() => {});
        }

    },
})